﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RCREEE.Library.Entities;
using System.Data;
using RCREEE.Web.Classes;


namespace RCREEE.Web
{
    public partial class Country_Details : System.Web.UI.Page
    {
        
        #region variable
        CountryDetails CD = new CountryDetails();
        DataTable DT = new DataTable();
        Project Project = new Project();
        int[] LanguageCode = { 1033, 1025, 1036 };
        List<CountryDetails> LCD = new List<CountryDetails>();
        DataTable dtPermissions = new DataTable();
	#endregion

        #region Methods
        public void Setvalues(DataTable Dt)
        {
            Rate.Text = Dt.Rows[0]["Rate"].ToString();
            Comment0.Text = Dt.Rows[0]["EnglishComment"].ToString();
            Comment1.Text = Dt.Rows[0]["ArabicComment"].ToString();
            Comment2.Text = Dt.Rows[0]["FrenchComment"].ToString();
        }

        public void SelectedIndexChanged()
        {
            if (Convert.ToInt32(Request["E"]) == 1)
            {
                try
                {
                    //DataSource = function of renewableEnergy
                    DT = CD.SelectCountryRenewableEnergey(Convert.ToInt32(DDCountry.SelectedValue));
                    Setvalues(DT);
                }
                catch (Exception ex) { }
            }
            //Chcek if Entity =2
            if (Convert.ToInt32(Request["E"]) == 2)
            {
                try
                {
                    // //DataSource = function of EnergyEfficiency
                    DT = CD.SelectCountryEnergyEfficiency(Convert.ToInt32(DDCountry.SelectedValue));
                    Setvalues(DT);
                }
                catch (Exception ex) { }
            }
        }

        public void refresh()
        {
            CountryDetailsView.Visible = true;
            EditButton.Visible = true;
            MultiView1.ActiveViewIndex = -1;
            DDCountry.SelectedValue = "0";
            SelectedIndexChanged();
            Lbpermission.Visible = false;
        }
        #endregion
       

        protected void Page_Load(object sender, EventArgs e)
        {//Set Session with LanguageCode

            if (!Page.IsPostBack)
            {
                MultiView1.ActiveViewIndex = -1;
       

            }
            if (Session["UserId"] == null)
                EditButton.Visible = false;
            // Create object from CountryDetails
            #region Table Permissions
            if (Session[SessionStrings.SS_Permissions] == null)
            {
                // Table declarations
                dtPermissions.Columns.Add("CountryId", typeof(int));
                dtPermissions.Columns.Add("CanUpdate", typeof(bool));
              

                // New Row .. assigning permissions
                DataRow drPermission = dtPermissions.NewRow();
                drPermission["CountryId"] = 1;
               
                drPermission["CanUpdate"] = false;
         

                dtPermissions.Rows.Add(drPermission);

                // Add table to the session object
                Session.Add("dtPermissions", dtPermissions);
            }
            else
            {
                dtPermissions = (DataTable)Session[SessionStrings.SS_Permissions];
                Session.Add("dtPermissions", dtPermissions);
            }
            #endregion

            if (Request["E"] != null)
            {
                //Check if Entity =1 

                if (Convert.ToInt32(Request["E"]) == 1)
                {
                    try
                    {
                        
                        //DataSource = function of renewableEnergy
                        CountryDetailsView.DataSource = (DataTable)CD.SelectRenewableEnergey(Convert.ToInt32(Session["LanguageCode"]));
                        CountryDetailsView.DataBind();
                    }
                    catch (Exception ex) { }
                }
                //Chcek if Entity =2
                if (Convert.ToInt32(Request["E"]) == 2)
                {
                    try
                    {
                       
                        // //DataSource = function of EnergyEfficiency
                        CountryDetailsView.DataSource = (DataTable)CD.SelectEnergyEfficiency(Convert.ToInt32(Session["LanguageCode"]));
                        CountryDetailsView.DataBind();
                    }
                    catch (Exception ex) { }
                }
            }
            else
            {
                CheckEntityLabel.Text = "Please,Specify Entity Type ";
                CheckEntityLabel.Visible = true;
            }
            if (DDCountry.SelectedValue != "" && DDCountry.SelectedValue != "0")
            {
                Save.Enabled = Can("Update", Convert.ToInt32(DDCountry.SelectedValue));
                Lbpermission.Visible = !Can("Update", Convert.ToInt32(DDCountry.SelectedValue));
            }
        
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            CountryDetailsView.Visible = false;
            EditButton.Visible = false;
        }

        protected void DDCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged();
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(Request["E"]) == 1)
            {
                try
                {
           for (int i = 0; i < 3; i++)
           {
               LCD.Add(new CountryDetails());
                LCD[i].CountryId = Convert.ToInt32(DDCountry.SelectedValue);
                LCD[i].LanguageCode = LanguageCode[i];
                LCD[i].RenewableEnergyContributionRate =float.Parse (Rate.Text);
                LCD[i].RenewableEnergyContributionRateComment = ((TextBox)Panel2.FindControl("Comment" + i)).Text;
            }
           CD.UpdateRenewable(LCD);
           CountryDetailsView.DataSource = (DataTable)CD.SelectRenewableEnergey(Convert.ToInt32(Session["LanguageCode"]));
           CountryDetailsView.DataBind();
                }
                catch (Exception ex) { }
            }
            //Chcek if Entity =2
            if (Convert.ToInt32(Request["E"]) == 2)
            {
                try
                {
                    for (int i = 0; i < 3; i++)
                    {
                        LCD.Add(new CountryDetails());
                        LCD[i].CountryId = Convert.ToInt32(DDCountry.SelectedValue);
                        LCD[i].LanguageCode = LanguageCode[i];
                        LCD[i].EnergyEfficiencyContributionRate = float.Parse(Rate.Text);
                        LCD[i].EnergyEfficiencyContributionRateComment = ((TextBox)Panel2.FindControl("Comment" + i)).Text;
                    }
                    CD.UpdateEnergy(LCD);
                     CountryDetailsView.DataSource = (DataTable)CD.SelectEnergyEfficiency(Convert.ToInt32(Session["LanguageCode"]));
                        CountryDetailsView.DataBind();
                    }
                
                catch (Exception ex) { }
            }

            refresh();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            refresh();
        }

        public bool Can(string permission, int CountryId)
        {
            if (Session["dtPermissions"] == null)
            {
                #region Table Permissions
                // Table declarations
                DataTable dtPermissions = new DataTable();

                dtPermissions.Columns.Add("CountryId", typeof(int));
         
                dtPermissions.Columns.Add("CanUpdate", typeof(bool));
            

                // New Row .. assigning permissions
                DataRow drPermission = dtPermissions.NewRow();
                drPermission["CountryId"] = 1;
             
                drPermission["CanUpdate"] = false;
                

                dtPermissions.Rows.Add(drPermission);

                Session.Remove("dtPermissions");

                // Add table to the session object
                Session.Add("dtPermissions", dtPermissions);
                #endregion
            }

            DataRow[] dr = ((DataTable)Session["dtPermissions"]).Select("CountryId = " + CountryId.ToString());

            if (dr.Length > 0)
            {
                return Convert.ToBoolean(dr[0]["Can" + permission]);
            }
            else
                return false;
        }

        }
    }
